package com.example.questionexam.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.questionexam.common.BaseResponse;
import com.example.questionexam.common.ErrorCode;
import com.example.questionexam.common.ResultUtils;
import com.example.questionexam.exception.BusinessException;
import com.example.questionexam.exception.ThrowUtils;
import com.example.questionexam.model.dto.exam.ExamAddRequest;
import com.example.questionexam.model.dto.exam.ExamQueryRequest;
import com.example.questionexam.model.dto.exam.ExamUpdateRequest;
import com.example.questionexam.model.entity.Exam;
import com.example.questionexam.service.ExamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Maobohe
 * @createData 2024/5/28 10:35
 */
@RestController
@RequestMapping("/exam")
@Slf4j
public class ExamController {

    @Resource
    private ExamService examService;

    /**
     * 创建考试权限
     *
     * @param examAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addExam(@RequestBody ExamAddRequest examAddRequest, HttpServletRequest request) {
        if (examAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Exam exam = new Exam();
        BeanUtils.copyProperties(examAddRequest, exam);
        // 校验
        examService.validExam(exam, true);
        boolean result = examService.save(exam);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(exam.getId());
    }


    /**
     * 更新Exam
     *
     * @param examUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateExam(@RequestBody ExamUpdateRequest examUpdateRequest,
                                               HttpServletRequest request) {
        if (examUpdateRequest == null || examUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Exam exam = new Exam();
        BeanUtils.copyProperties(examUpdateRequest, exam);
        // 校验
        examService.validExam(exam, false);
        QueryWrapper<Exam> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", exam.getId());
        Exam old = examService.getOne(queryWrapper);
        ThrowUtils.throwIf(old == null, ErrorCode.NOT_FOUND_ERROR);

        boolean result = examService.update(exam, queryWrapper);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 根据id获取老师
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Exam> getExamById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Exam exam = examService.getById(id);
        ThrowUtils.throwIf(exam == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(exam);
    }

    /**
     * 分页获取试卷列表
     * @param examQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<Exam>> listExamByPage(ExamQueryRequest examQueryRequest,
                                                         HttpServletRequest request) {
        long current = examQueryRequest.getCurrent();
        long size = examQueryRequest.getPageSize();
        Page<Exam> examPage = examService.page(new Page<>(current, size),
                examService.getQueryWrapper(examQueryRequest));
        return ResultUtils.success(examPage);
    }


}


